//
//  firstViewController.m
//  baohebao
//
//  Created by ming wang on 12-2-19.
//  Copyright (c) 2012年 mingrui. All rights reserved.
//

#import "firstViewController.h"

@implementation firstViewController
@synthesize button;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationController.navigationBarHidden = YES;
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
    }
    return self;
}



- (IBAction)login:(id)sender{
    [tf resignFirstResponder];
    if ([tf.text length]<=0) {
        return;
    }
    [theAppDelegate showActivityView:YES text:@"保合宝，让我们一起见证互联网改变保险！"];
//    NSString * result = [NSString stringWithFormat:@"%@", [[[MyClass getInstance] validatedUser] objectForKey:@"code"]];
//    if ([result intValue] == 0) {
//        [theAppDelegate OnLoginView:NO];
//        return;
//    }
    NSDictionary * ret = [[MyClass getInstance] generateToken:tf.text];
    [theAppDelegate showActivityView:NO text:@""];
    NSString * msg = [NSString stringWithFormat:@"%@", [ret objectForKey:@"code"]]; 
    UIAlertView *alert;
    switch ([msg intValue]) {
        case 0:
        {
            FirstLauchViewController* firstView = [[FirstLauchViewController alloc]init];
            [self.navigationController pushViewController:firstView animated:YES];
            [firstView release];
            /*
             alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"保合宝客户端申请已经发送到您的用户名下，请登录 baohebao.com，予以确认。" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
            optType = 9;
            [alert show];
            [alert release];
            */
        }
            break;
        case -1:
             alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"网络错误，请检查您的网络连接！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            optType = 1;
            [alert show];
            [alert release];
            break;
        case -2:
            alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:[ret objectForKey:@"error"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            optType = 1;
            [alert show];
            [alert release];
            break;
//        case 4:
//            msg = [NSString stringWith    Format:@"%@, %@", msg, [ret objectForKey:@"error"]];
//            
//            UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"令牌失效，请登陆www.bbest100.com重新激活您的账号！" delegate:self cancelButtonTitle:@"以后再说" otherButtonTitles:@"前往激活", nil];
//            optType = 2;
//            [alert2 show];
//            [alert2 release];
//            break;
        default:
            if ([msg intValue]==13) {
                optType = 13;
            }
            else
                optType = 1;
            msg = [NSString stringWithFormat:@"%@", [ret objectForKey:@"error"]];
            
            alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
            break;
    }

    
}

-(void)validatedUser {
    NSDictionary * ret = [[MyClass getInstance] validatedUser];
    NSString * msg = [NSString stringWithFormat:@"%@", [ret objectForKey:@"code"]];
    if ([msg intValue] != 0) {
        switch ([msg intValue]) {
            case -1:
                msg = [NSString stringWithFormat:@"%@, %@", msg, [ret objectForKey:@"error"]];
                
                UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"网络连接失败，请检查网络连接！" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
                optType = 1;
                [alert1 show];
                [alert1 release];
                break;
            case -2:
                msg = [NSString stringWithFormat:@"%@, %@", msg, [ret objectForKey:@"error"]];
                
                UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"未知错误,请联系客服人员！" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
                optType = 1;
                [alert2 show];
                [alert2 release];
                break;
            case -4:
                msg = [NSString stringWithFormat:@"%@, %@", msg, [ret objectForKey:@"error"]];
                
                UIAlertView *alert4= [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"令牌需要更换，请重新更换令牌！" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
                optType = 1;
                [alert4 show];
                [alert4 release];
                break;
            default:
                msg = [NSString stringWithFormat:@"%@, %@", msg, [ret objectForKey:@"error"]];
                
                UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您的账号还未激活，请登陆www.bbest100.com激活您的账号！" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
                optType = 2;
                [alert3 show];
                [alert3 release];
                break;
        }
    }
    else
    {
        [theAppDelegate OnLoginView:NO];
    }
}
-(void) exit{
    exit(0);
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if ( buttonIndex == 0 ) 
    {
        if (optType==9) {
             [theAppDelegate showActivityView:NO text:@" "];
            [self performSelector:@selector(exit) withObject:nil afterDelay:0.1];
            //[self exit];   
        }
        else if (optType==13) {
             [theAppDelegate showActivityView:NO text:@" "];
            [self performSelector:@selector(exit) withObject:nil afterDelay:0.1];
        }
        else 
        {
             [theAppDelegate showActivityView:NO text:@" "];
        }
        
    }
    else
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baohebao.com"]];
    }
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    tf.delegate = self;
    // Do any additional setup after loading the view from its nib.
//    NSString * strUser =   [[MyClass getInstance] getStoredName];
//    if ([strUser length]>0)
//    {
//        [self validatedUser];
//    }
    button.layer.cornerRadius = 10;/////////////////////////自己试着改变这个值，看看各个不同的效果。现在的设置是画了个圆圈。
    button.layer.masksToBounds = YES;//////////////////////////    
}

- (void)viewDidUnload
{
    [self setButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)baohebaoApploginCallback:(NSNotification *)_notification
{
    [theAppDelegate OnLoginView:NO];
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(baohebaoApploginCallback:) 
                                                 name:@"baohebaoApplogin"
                                               object:nil];
};
-(void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"baohebaoApplogin" object:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [button release];
    [super dealloc];
}
@end
